<template>
  <div class="device-category">
    <!-- 搜索和添加按钮区域 -->
    <div class="operation-area">
      <el-input v-model="queryParams.typeName" placeholder="请输入设备类型名称搜索" style="width: 200px" @input="handleSearch" />
      <el-button type="primary" @click="handleAdd">添加设备类型</el-button>
    </div>

    <!-- 表格区域 -->
    <el-table :data="tableData" style="width: 100%;height: 690px;">
      <el-table-column align="center" type="index" width="60" />
      <el-table-column prop="typeName" align="center" width="200" label="设备类型名称" />
      <el-table-column prop="typeStatus" align="center" width="120" label="设备类型状态">
        <template #default="{ row }">
          <el-tag :type="row.typeStatus == 1 ? 'success' : 'danger'">
            {{ row.typeStatus == 1 ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="active" align="center" label="是否可激活" width="120">

        <template #default="{ row }">
          <el-tag>
            {{ row.active == 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="content" align="center" label="激活模版" />

      <el-table-column label="操作" align="center" width="180">
        <template #default="scope">
          <el-button type="primary" link size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" link size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <!-- <el-pagination :current-page="currentPage" :page-size="pageSize" :total="total" layout="total, prev, pager, next"
      @current-change="handleCurrentChange" /> -->

    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :close-on-click-modal="false" draggable :title="dialogTitle" width="50%">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="设备类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入设备类型名称" />
        </el-form-item>
        <el-form-item label="设备类型状态" prop="typeStatus">
          <el-radio-group v-model="form.typeStatus">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否可激活" prop="active">
          <el-radio-group v-model="form.active">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="激活模版" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入激活模版" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import DeviceAPI from '@/api/system/device.api'
// 表格数据
const tableData = ref([])
const searchQuery = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const queryParams = ref({
  typeName: ''
})
// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formRef = ref(null)
const form = reactive({
  id: undefined,
  typeName: '',
  typeStatus: 1,
  active: 1,
  content: ''
})

// 表单验证规则
const rules = {
  name: [
    { required: true, message: '请输入设备类型名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  description: [
    { max: 200, message: '描述不能超过200个字符', trigger: 'blur' }
  ]
}

// 搜索处理
const handleSearch = () => {
  fetchData()
}

// 获取表格数据
const fetchData = async () => {
  const res = await DeviceAPI.getdevType(queryParams.value)
  console.log('设备类型', res);
  tableData.value = res.data.data
}

// 添加设备类型
const handleAdd = () => {
  dialogTitle.value = '添加设备类型'
  form.typeName = ''
  form.id = undefined
  form.typeStatus = 1
  form.active = 1
  form.content = ''
  dialogVisible.value = true
}

// 编辑设备类型
const handleEdit = (row) => {
  dialogTitle.value = '编辑设备类型'
  form.typeName = row.typeName
  form.id = row.id
  form.typeStatus = row.typeStatus
  form.active = row.active
  form.content = row.content

  dialogVisible.value = true
}

// 删除设备类型
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该设备类型吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      // TODO: 调用后端API删除数据
      await DeviceAPI.deleteType(row.id)
      ElMessage.success('删除成功')
      fetchData()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        // TODO: 调用后端API保存数据
        if (form.id) {
          await DeviceAPI.updateType(form)
        } else {
          await DeviceAPI.createType(form)
        }
        ElMessage.success(form.id ? '更新成功' : '添加成功')
        dialogVisible.value = false
        fetchData()
      } catch (error) {
        ElMessage.error(form.id ? '更新失败' : '添加失败')
      }
    }
  })
}

// 分页变化
const handleCurrentChange = (page) => {
  currentPage.value = page
  fetchData()
}

// 初始化加载数据
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.device-category {
  padding: 20px;
}

.operation-area {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
